package com.xs.codec;

import com.xs.codec.entity.Header;
import com.xs.codec.entity.Message;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class CustomEncoder extends MessageToByteEncoder<Message> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
        if(msg == null || msg.getHeader() == null){
            throw new Exception("the encode message is null");
        }
        Header header = msg.getHeader();
        int tag = header.getTag();
        int commandCode = header.getCommandCode();
        int version = header.getVersion();
        int length = header.getLength();
        
        Object data = msg.getData();
        
        out.writeInt(tag);
        out.writeInt(commandCode);
        out.writeInt(version);
        out.writeInt(0);
        
        if(data != null){
            String str = (String) data;
            byte[] bytes = str.getBytes();
            length = bytes.length;
            out.writeBytes(bytes);
        }
        //写入length;
        out.setInt(12, length);
    }
}
